﻿namespace AppLab.Web
{
    using System;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Data.Objects.DataClasses;
    using System.Linq;
    using AppLab.Data.Models;
    using AppLab.Web.Resources;
    using Wicresoft.AppFlow.Flows;

    public class MobileBasicInfoData
    {
        [Key]
        [Display(AutoGenerateField = false)]
        public Guid FlowGuid { get; set; }
        [Display(AutoGenerateField = false)]
        public int MobileID { get; set; }

        #region DataGridFieldValues

        [Display(AutoGenerateField = false)]
        public string MobileBrand { get; set; }
        [Display(AutoGenerateField = false)]
        public string MobileType { get; set; }
        [Display(AutoGenerateField = false)]
        public string MobileOS { get; set; }
        [Display(AutoGenerateField = false)]
        public string Resoulution { get; set; }
        [Display(AutoGenerateField = false)]
        public string ScreenSize { get; set; }
        [Display(AutoGenerateField = false)]
        public string OutwardTypeStr { get; set; }
        [Display(AutoGenerateField = false)]
        public string IsOn3G { get; set; }
        [Display(AutoGenerateField = false)]
        public string IsOnStreamMedia { get; set; }
        [Display(AutoGenerateField = false)]
        public string IsOnTouch { get; set; }
        [Display(AutoGenerateField = false)]
        public DateTime? InformationTime { get; set; }

        #endregion

        #region ModifyField

        [Display(AutoGenerateField = false)]
        public int BrandID { get; set; }
        //型号
        [Display(AutoGenerateField = false)]
        public int CategoryID { get; set; }
        [Display(AutoGenerateField = false)]
        public int OSID { get; set; }
        [Display(AutoGenerateField = false)]
        public int PlatformID { get; set; }
        [Display(AutoGenerateField = false)]
        public int ScreenSizeID { get; set; }
        [Display(AutoGenerateField = false)]
        public int ResoulutionID { get; set; }
        [Display(AutoGenerateField = false)]
        public int CPUID { get; set; }
        [Display(AutoGenerateField = false)]
        public int OutwardTypeID { get; set; }
        [Display(AutoGenerateField = false)]
        public int Is3G { get; set; }
        [Display(AutoGenerateField = false)]
        public int IsTouch { get; set; }
        [Display(AutoGenerateField = false)]
        public int IsStreamMedia { get; set; }
        [Display(AutoGenerateField = false)]
        public int Prince { get; set; }
        #endregion

        [Display(AutoGenerateField = false)]
        public string UpdateUser { get; set; }
        [Display(AutoGenerateField = false)]
        public string CreateUser { get; set; }

        public void UpadteMobileBasicInfoData()
        {
            using (var dc = PMSEntities.Create())
            {
                var MobileBasicInfo = dc.MobileBasicInfoes.First(p => p.MobileID == this.MobileID);
                {
                    MobileBasicInfo.BrandID = BrandID;
                    MobileBasicInfo.MobileType = MobileType.Replace(" ", "");
                    MobileBasicInfo.CategoryID = CategoryID;
                    MobileBasicInfo.CPUID = CPUID;
                    MobileBasicInfo.ScreenSizeID = ScreenSizeID;
                    MobileBasicInfo.ResolutionID = ResoulutionID;
                    MobileBasicInfo.OSID = OSID;
                    MobileBasicInfo.PlatformID = PlatformID;
                    MobileBasicInfo.OutwardTypeID = OutwardTypeID;
                    MobileBasicInfo.IS3G = Is3G;
                    MobileBasicInfo.StreamMedia_ID = IsStreamMedia;
                    MobileBasicInfo.Touch_ID = IsTouch;
                    MobileBasicInfo.Price = Prince;
                    MobileBasicInfo.UpdateUser = UpdateUser;
                    MobileBasicInfo.InformationTime = InformationTime;
                    dc.SaveChanges();
                };
            }
        }

        public void AddMobileBasicInfoData()
        {
            using (var dc = PMSEntities.Create())
            {
                var e = new MobileBasicInfo();

                e.BrandID = BrandID;
                e.MobileType = MobileType.Replace(" ", "");
                e.CategoryID = CategoryID;
                e.CPUID = CPUID;
                e.ScreenSizeID = ScreenSizeID;
                e.ResolutionID = ResoulutionID;
                e.IS3G = Is3G;
                e.OSID = OSID;
                e.PlatformID = PlatformID;
                e.OutwardTypeID = OutwardTypeID;
                e.Price = Prince;
                e.Touch_ID = IsTouch;
                e.StreamMedia_ID = IsStreamMedia;
                e.CreateUser = CreateUser;

                if (this.InformationTime.HasValue)
                {
                    e.InformationTime = InformationTime;
                }

                dc.MobileBasicInfoes.AddObject(e);
                dc.SaveChanges();

                int i = 0;
                int iMobileID = e.MobileID;
                MoblieAdaptationInfo ma1 = new MoblieAdaptationInfo();
                ma1.AdaptationTypeID = 1;
                ma1.MobileID = iMobileID;
                ma1.AdaptationStatusID = 1;
                ma1.AdaptationTime = DateTime.Now;
                ma1.AdaptationBeginTime = DateTime.Now;
                ma1.AdaptationEndTime = DateTime.Now;
                dc.MoblieAdaptationInfoes.AddObject(ma1);
                i = dc.SaveChanges();
                MoblieAdaptationInfo ma2 = new MoblieAdaptationInfo();
                ma2.AdaptationTypeID = 2;
                ma2.MobileID = iMobileID;
                ma2.AdaptationStatusID = 1;
                ma2.AdaptationTime = DateTime.Now;
                ma2.AdaptationBeginTime = DateTime.Now;
                ma2.AdaptationEndTime = DateTime.Now;
                dc.MoblieAdaptationInfoes.AddObject(ma2);
                i = dc.SaveChanges();
            }
        }
    }
}